using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Payment : System.Web.UI.UserControl
{
    processPaymentType pPayment;
    protected void Page_Load(object sender, EventArgs e)
    {
        pPayment = new processPaymentType();

        if (!Page.IsPostBack)
        {
            Load_gvwPayment();   
        }
    }
    private void Load_gvwPayment()
    {
        gvwPayment.DataSource = pPayment.getPaymentType();
        gvwPayment.DataBind();
    }
    protected void gvwPayment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwPayment.PageIndex = e.NewPageIndex;
        Load_gvwPayment();
    }
    protected void gvwPayment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvwPayment.EditIndex = -1;
        Load_gvwPayment();
    }
    protected void gvwPayment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string PaymentTypeID = gvwPayment.DataKeys[e.RowIndex].Value.ToString();
            if (pPayment.deletePaymentType(PaymentTypeID) > 0)
            {
                Load_gvwPayment();
            }
        }
        catch (Exception)
        {
            Alert.Show("This paymney type is being used, you can not delete!");
        }  
    }
    protected void gvwPayment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvwPayment.EditIndex = e.NewEditIndex;
        Load_gvwPayment();
    }
    protected void gvwPayment_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string PaymentTypeID = gvwPayment.DataKeys[e.RowIndex].Value.ToString();
        string PaymentTypeName = ((TextBox)gvwPayment.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

        if (pPayment.updatePaymentType(PaymentTypeID, PaymentTypeName) > 0)
        {
            gvwPayment.EditIndex = -1;
            Load_gvwPayment();
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string PaymentTypeID = pPayment.getPaymentTypeID();
        if (pPayment.insertPaymentType(PaymentTypeID, "") > 0)
        {
            gvwPayment.EditIndex = gvwPayment.Rows.Count;
            Load_gvwPayment();
        }
    }
}
